﻿using UnityEngine;
using System.Collections;

public class bouncer : MonoBehaviour {

	private float vel = 0f;
	public float bouncy = 1f;
	public int hitpoints = -1; // -1 = infinite
	
	
	
	void Update()
	{
		float vel2 = rigidbody2D.velocity.magnitude;
		
		if(vel2 > vel)
			vel = vel2;
	}

	
	void OnCollisionEnter2D (Collision2D c) {
		
		rigidbody2D.velocity = rigidbody2D.velocity.normalized+c.contacts[0].normal;
		
		//rigidbody2D.velocity += c.contacts[0].normal*rigidbody2D.velocity.magnitude;
		
		if(rigidbody2D.velocity.magnitude > 0f)
		{
			rigidbody2D.velocity = rigidbody2D.velocity.normalized*vel*bouncy;
		}
		else
			Destroy(gameObject);
		
		//transform.position += (Vector3)rigidbody2D.velocity;
	
		ParticleSystem party = gameObject.GetComponent<ParticleSystem>();
		if(party != null)
			party.Play();
			
		if(hitpoints>-1)
		{
			hitpoints--;
			
			if(hitpoints == 0)
			{
				Destroy(gameObject);
			}
		}
	
	}
}
